#!/bin/bash

# Define constants
GROUP_ID="com.ly.smart-doc"
ARTIFACT_ID="smart-doc-maven-plugin"
VERSION="3.0.3"
CONFIG_FILE="./src/main/resources/smart-doc.json"
INCLUDES=("com.baomidou:mybatis-plus-extension" "com.baomidou:mybatis-plus-core" "org.springframework.data:spring-data-commons")

# Define the command to add smart-doc-maven-plugin
ADD_PLUGIN_COMMAND="xmlstarlet ed --inplace -N x=http://maven.apache.org/POM/4.0.0 \
  --subnode \"/x:project/x:build/x:plugins\" --type elem -n plugin -v \"\" \
  --subnode \"/x:project/x:build/x:plugins/plugin[last()]\" --type elem -n groupId -v \"$GROUP_ID\" \
  --subnode \"/x:project/x:build/x:plugins/plugin[last()]\" --type elem -n artifactId -v \"$ARTIFACT_ID\" \
  --subnode \"/x:project/x:build/x:plugins/plugin[last()]\" --type elem -n version -v \"$VERSION\""

# Delete version element while add to submodule pom.xml
SUBMODULE_ADD_PLUGIN_COMMAND="xmlstarlet ed --inplace -N x=http://maven.apache.org/POM/4.0.0 \
  --subnode \"/x:project/x:build/x:plugins\" --type elem -n plugin -v \"\" \
  --subnode \"/x:project/x:build/x:plugins/plugin[last()]\" --type elem -n groupId -v \"$GROUP_ID\" \
  --subnode \"/x:project/x:build/x:plugins/plugin[last()]\" --type elem -n artifactId -v \"$ARTIFACT_ID\""

ADD_PLUGIN_MANAGEMENT_COMMAND="xmlstarlet ed --inplace -N x=http://maven.apache.org/POM/4.0.0 \
  --subnode \"/x:project/x:build/x:pluginManagement/x:plugins\" --type elem -n plugin -v \"\" \
  --subnode \"/x:project/x:build/x:pluginManagement/x:plugins/plugin[last()]\" --type elem -n groupId -v \"$GROUP_ID\" \
  --subnode \"/x:project/x:build/x:pluginManagement/x:plugins/plugin[last()]\" --type elem -n artifactId -v \"$ARTIFACT_ID\" \
  --subnode \"/x:project/x:build/x:pluginManagement/x:plugins/plugin[last()]\" --type elem -n version -v \"$VERSION\""

# Check if it is a multi-module Maven project
MODULES=$(xmlstarlet sel -N x=http://maven.apache.org/POM/4.0.0 -t -c "/x:project/x:modules" pom.xml)

if [ -z "$MODULES" ]; then
    echo "This is a single module project."

    PLUGIN=$(xmlstarlet sel -N x=http://maven.apache.org/POM/4.0.0 -t -c "/x:project/x:build/x:plugins/x:plugin[x:groupId='$GROUP_ID' and x:artifactId='$ARTIFACT_ID']" pom.xml)

    if [ -z "$PLUGIN" ]; then
        eval "$ADD_PLUGIN_COMMAND pom.xml"
        eval "xmlstarlet fo -s 4 pom.xml > pom.xml.tmp && mv pom.xml.tmp pom.xml"
        echo "The plugin has been added to the XML document."
    else
        echo "The plugin is already present in the XML document."
    fi

else
    echo "This is a multi-module project."

    PLUGIN=$(xmlstarlet sel -N x=http://maven.apache.org/POM/4.0.0 -t -c "/x:project/x:build/x:pluginManagement/x:plugins/x:plugin[x:groupId='$GROUP_ID' and x:artifactId='$ARTIFACT_ID']" pom.xml)

    if [ -z "$PLUGIN" ]; then
        eval "$ADD_PLUGIN_MANAGEMENT_COMMAND pom.xml"
        eval "xmlstarlet fo -s 4 pom.xml > pom.xml.tmp && mv pom.xml.tmp pom.xml"
        echo "The plugin has been added to the pluginManagement section."
    else
        echo "The plugin is already present in the pluginManagement section."
    fi

    SUBMODULES_POM=$(find . -mindepth 1 -path "./pom.xml" -prune -o -name "pom.xml" -print | sort)
    # shellcheck disable=SC2145
    for SUBMODULE_POM in $SUBMODULES_POM; do
        MODULE_NAME=$(xmlstarlet sel -N x="http://maven.apache.org/POM/4.0.0" -t -v "/x:project/x:artifactId" "$SUBMODULE_POM")
        PLUGIN=$(xmlstarlet sel -N x=http://maven.apache.org/POM/4.0.0 -t -c "/x:project/x:build/x:plugins/x:plugin[x:groupId='$GROUP_ID' and x:artifactId='$ARTIFACT_ID']" "$SUBMODULE_POM")
        if [ -z "$PLUGIN" ]; then
            eval "$SUBMODULE_ADD_PLUGIN_COMMAND $SUBMODULE_POM"
            eval "xmlstarlet fo -s 4 $SUBMODULE_POM > $SUBMODULE_POM.tmp && mv $SUBMODULE_POM.tmp $SUBMODULE_POM"
            echo "The plugin has been added to the $MODULE_NAME module."
        else
            echo "The plugin is already present in the $MODULE_NAME module."
        fi
    done
fi
